package cn.iocoder.yudao.module.brokerage.merchant.controller.app;

import cn.iocoder.yudao.framework.business.basic.pojo.bo.ExtraPageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.framework.validation.groups.QueryGroup;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AppBrokerageMerchantRecordQueryDTO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AppBrokerageMerchantRecordVO;
import cn.iocoder.yudao.module.brokerage.merchant.service.AppBrokerageMerchantRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 推广商家分佣记录")
@RestController
@RequestMapping("/brokerage/merchant/record")
@Validated
public class AppBrokerageMerchantRecordController {
    @Resource
    private AppBrokerageMerchantRecordService appBrokerageMerchantRecordService;

    @PreAuthorize("@mm.isAgency()")
    @Operation(summary = "分页")
    @Parameter(name = "pageNo", description = "页码", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", required = true, example = "10")
    @GetMapping("/page")
    CommonResult<ExtraPageResult<AppBrokerageMerchantRecordVO>> getPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @Validated(value = QueryGroup.class) AppBrokerageMerchantRecordQueryDTO param) {
        Long userId = getLoginUserId();
        PageResult<AppBrokerageMerchantRecordVO> page = appBrokerageMerchantRecordService.getPage(pageNo, pageSize, param, userId);
        ExtraPageResult<AppBrokerageMerchantRecordVO> pageX = new ExtraPageResult<>(page.getList(), page.getTotal());
        pageX.put("numberSum", MoneyUtils.fenToYuanStr(appBrokerageMerchantRecordService.getNumberSum(userId, param)));
        return success(pageX);
    }

}